当你的代码库超过几百行时,它将从一个简单的脚本转变为一个 系统。为了防止认知过载,Rust 使用分层的 模块系统 来将功能划分为逻辑清晰、易于管理的边界。
1. 可扩展性的必要性
在一个庞大的系统中,你不必将整个架构都记在脑子里。模块可以帮助你隔离实现细节,仅通过公共 API 暴露必要的部分。
2. 双 Crate 架构
一个单一的 Rust 包 作为容器。它可以同时容纳一个库 Crate(src/lib.rs)用于核心逻辑,以及一个二进制 Crate(src/main.rs)用于可执行程序的入口点。这确保了 系统做什么 与 用户如何与系统交互 之间的清晰分离。
3. 组织基础
通过使用 cargo new --lib来初始化项目,你优先考虑模块化设计。在餐厅管理系统中,“前台”(接待)和“后台”(烹饪)被划分开来,允许多个前端(命令行、网页、移动端)共享相同的底层库逻辑。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
By default, what is the visibility of items (functions, modules) in Rust?
Public to the entire package.
Private to the parent module.
Public within the crate root only.
Accessible via 'super' only.
✅ Correct!
Rust follows the principle of encapsulation: everything is private by default unless explicitly marked with 'pub'.❌ Incorrect
Rust defaults to privacy to protect implementation details from accidental outside dependency.QUESTION 2
Which file is considered a 'Crate Root' by convention for a library?
src/main.rs
$src/mod.rs$
$src/lib.rs$
Cargo.toml
✅ Correct!
src/lib.rs is the standard entry point for library crates.❌ Incorrect
src/main.rs is for binaries; Cargo.toml is the manifest, not a source file.QUESTION 3
Can a single Rust package contain both a library and a binary crate?
No, it must be one or the other.
Yes, by having both src/lib.rs and src/main.rs.
Yes, but they must share the same file name.
Only if defined in separate Cargo workspaces.
✅ Correct!
This is a common pattern: the library contains logic, and the binary provides the interface.❌ Incorrect
Rust packages are designed to support multiple crates of different types (lib vs bin).QUESTION 4
What is the purpose of the 'crate' keyword in a path?
To access an external dependency.
To start an absolute path from the crate root.
To define a new module.
To import a library from Crates.io.
✅ Correct!
Using 'crate::' allows you to navigate the module tree from the very top.❌ Incorrect
External dependencies are handled by their names; 'crate' refers to the current crate's root.QUESTION 5
Which command initializes a new project focused on reusable library logic?
cargo new project_name
cargo build --lib
cargo new project_name --lib
cargo init --bin
✅ Correct!
The --lib flag tells Cargo to create a src/lib.rs instead of a src/main.rs.❌ Incorrect
'cargo new' without flags defaults to a binary project.Architectural Design: Restaurant System
Applying module theory to a scaling codebase.
You are designing a restaurant management library. You have a module 'back_of_house' that handles 'cooking'. You need to call 'cook_order' from an external binary that imports your library.
Q
1. If 'back_of_house' is a module in src/lib.rs, how must 'cook_order' be declared to be reachable by the binary?
Solution:
Both the module 'back_of_house' and the function 'cook_order' must be marked with the 'pub' keyword. Marking a parent public does not automatically make children public.
Both the module 'back_of_house' and the function 'cook_order' must be marked with the 'pub' keyword. Marking a parent public does not automatically make children public.
Q
2. Why is it beneficial to put the cooking logic in a library crate instead of directly in the main binary?
Solution:
It promotes reusability. By putting logic in src/lib.rs, it can be shared by multiple binaries (like a kitchen display app and a mobile ordering app) and allows for easier unit testing.
It promotes reusability. By putting logic in src/lib.rs, it can be shared by multiple binaries (like a kitchen display app and a mobile ordering app) and allows for easier unit testing.